package ar.edu.utn.frsf.cidisi.ginet.diagram.part;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.pnml.tools.epnk.pnmlcoremodel.Page;
import org.pnml.tools.epnk.pnmlcoremodel.PetriNet;
import org.pnml.tools.epnk.pnmlcoremodel.PetriNetDoc;
import org.pnml.tools.epnk.pnmlcoremodel.PetriNetType;

import ar.edu.utn.frsf.cidisi.ginet.GINet;
import ar.edu.utn.frsf.cidisi.ginet.generation.pnml2cpntools.Pnml2CpnTools;




/**
 * @generated
 */
public class GenerateCPNFileAction implements IObjectActionDelegate {


	private GINet ginet;
	
	PetriNetType type;
	
	private Page concreteGIModule;
	
	
	/**
	 * @generated
	 */
	private IWorkbenchPart targetPart;

	/**
	 * @generated
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		this.targetPart = targetPart;
	}

	/**
	 * @generated
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		ginet = null;
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			if (structuredSelection.size() == 1
					&& structuredSelection.getFirstElement() instanceof Page) {
//				((Page)structuredSelection.getFirstElement()).eContainer();
				concreteGIModule = (Page)structuredSelection.getFirstElement();
				ginet = (GINet)concreteGIModule.eContainer();
			}
		}
//		action.setEnabled(isEnabled(petrinet));	}
	}

	/**
	 * @generated
	 */
	private Shell getShell() {
		return targetPart.getSite().getShell();
	}

	/**
	 * @generated
	 */
	public void run(IAction action) {
		
		createPages((PetriNetDoc)ginet.eContainer());
		
			MessageDialog
			.openInformation(
					getShell(),
					"UP-ColBPIP",
					"Petri Net specification was generated on ");// + ip.getName());
//		}
		
	}
	
	private void createPages(PetriNetDoc pnDoc) {
		String path = ginet.eResource().getURI().toPlatformString(true);
		String fileName = ginet.eResource().getURI().lastSegment();
		String newFileName = path.substring(0, path.indexOf(fileName)) + "cgim.cpn";

		Pnml2CpnTools pnml2cpn = new Pnml2CpnTools(ginet);
		pnml2cpn.generateCPN();
		pnml2cpn.export(ResourcesPlugin.getWorkspace().getRoot().getLocation()+newFileName);
		try {
			IContainer container = ResourcesPlugin.getWorkspace().getRoot().getContainerForLocation(new Path(ResourcesPlugin.getWorkspace().getRoot().getLocation()+path.substring(0, path.indexOf(fileName))));
			container.refreshLocal(IContainer.DEPTH_INFINITE, null);
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
	}

	
}
